package com.fieldchain.ex.fa100.utils.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * Created by 123 on 2018/4/23.
 */

public class TimeUtils {
    //字符串转时间戳
    public static String getTime(String timeString){
        String timeStamp = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
        Date d;
        try{
            d = sdf.parse(timeString);
            long l = d.getTime();
            timeStamp = String.valueOf(l);
        } catch(ParseException e){
            e.printStackTrace();
        }
        return timeStamp;
    }

    //时间戳转字符串
    public static String getStrTime(String timeStamp){
        String timeString = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd- HH:mm");
        long  l = Long.valueOf(timeStamp);
        timeString = sdf.format(new Date(l));//单位秒
        return timeString;
    }
    //时间戳转字符串
    public static String getSimpleTime(String timeStamp){
        String timeString = null;
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");
        long  l = Long.valueOf(timeStamp);
        timeString = sdf.format(new Date(l));//单位秒
        return timeString;
    }
    //时间戳转字符串
    public static String getSimpleTime2(String timeStamp){
        String timeString = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long  l = Long.valueOf(timeStamp);
        timeString = sdf.format(new Date(l));//单位秒
        return timeString;
    }


    //时间戳转字符串
    public static String getSimpleTime3(String timeStamp){
        String timeString = null;
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        long  l = Long.valueOf(timeStamp)*1000;
        timeString = sdf.format(new Date(l));//单位秒
        return timeString;
    }

    //时间戳转字符串
    public static String getSimpleTime4(Long timeStamp){
        String timeString = null;
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss",Locale.CHINESE);
        timeString = sdf.format(new Date(timeStamp));//单位秒
        return timeString;
    }

    /**
     * @return -1 ：同一天.    0：昨天 .   1 ：至少是前天.
     * @throws ParseException 转换异常
     */
    public static int isCurrrenDay(String timeStamp){
        long  l = Long.valueOf(timeStamp)*1000;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String todayStr = format.format(new Date());
        Date today = null;
        Date oldTime=new Date(l);
        try {
            today = format.parse(todayStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //昨天 86400000=24*60*60*1000 一天
        if((today.getTime()-oldTime.getTime())>0 && (today.getTime()-oldTime.getTime())<=86400000) {
            return 2;
        }
        else if((today.getTime()-oldTime.getTime())<=0){ //至少是今天
            return 1;
        }
        else{ //至少是前天
            return 0;
        }

    }
}
